/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mantechhelpdesk.bean;

import java.sql.Connection;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import mantechhelpdesk.dao.AnswersDao;
import mantechhelpdesk.dao.ComplaintsDao;
import mantechhelpdesk.dao.DbConnection;
import mantechhelpdesk.entity.Answers;
import mantechhelpdesk.entity.Complaints;

/**
 *
 * @author MANHDD
 */
public class AnswersBean {

    private Answers answers = null;
    private List<Answers> answersList;
    private AnswersDao answersDao;
    private Connection conn;

    public Answers getAnswers() {
        return answers;
    }

    public void setAnswers(Answers answers) {
        this.answers = answers;
    }

    public List<Answers> getAnswersList() {
        return answersList;
    }

    public void setAnswersList(List<Answers> answersList) {
        this.answersList = answersList;
    }
    
    
    public AnswersBean() {
        try {
            answersDao = new AnswersDao();
            answers = new Answers();
            conn = DbConnection.getConnection();
            answersList = answersDao.loadAll(conn);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    
    
    public String addNew() {
        FacesContext ct = FacesContext.getCurrentInstance();
        try {
            if (answers.getAnswerContent().length() > 0) {
                Calendar now = GregorianCalendar.getInstance();
                answers.setCreateTime(now.getTime());
                answersDao.create(conn, answers);
                answersList = answersDao.loadAll(conn);
                ct.addMessage("", new FacesMessage("Answer addnew Success!"));
            } else {
                ct.addMessage("", new FacesMessage("Required to input a Answer name!"));
                return "addNewError";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            ct.addMessage("", new FacesMessage("Cannot addnew Answer!"));
        }
        return "addNewSuccess";
    }
    
    
    
    public String update() {
        FacesContext ct = FacesContext.getCurrentInstance();
        try {
            if (answers.getAnswerContent().length() > 0) {
                answersDao.save(conn, answers);
                answersList = answersDao.loadAll(conn);
                ct.addMessage("", new FacesMessage("Answer update Success!"));
            } else {
                ct.addMessage("", new FacesMessage("Required to input a Answer name!"));
                return "answersUpdateError";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            ct.addMessage("", new FacesMessage("Cannot update Answer!"));
        }
        return "updateSuccess";
    }

    public String delete() {
        FacesContext ct = FacesContext.getCurrentInstance();
        try {
            answersDao.delete(conn, answers);
            ct.addMessage("", new FacesMessage("Answer delete Success!"));  
            answersList = answersDao.loadAll(conn);
        } catch (Exception ex) {
            ex.printStackTrace();
            ct.addMessage("", new FacesMessage("Cannot delete Answer!"));
        }
        return "delBackToAnswerList";
    }
    
    public String prepareData4Add() {
        try {
            FacesContext ct = FacesContext.getCurrentInstance();
            Map<String, String> map = ct.getExternalContext().getRequestParameterMap();
            String id = map.get("idParam");
            answers.setComplaintID(Integer.parseInt(id));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "detailComplaint";
    }

    public String prepareData4Update() {
        try {
            FacesContext ct = FacesContext.getCurrentInstance();
            Map<String, String> map = ct.getExternalContext().getRequestParameterMap();
            String id = map.get("idParam");
            answers = answersDao.getObject(conn, Integer.parseInt(id));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "updateAnswer";
    }

    public String prepareData4Delete() {
        try {
            FacesContext ct = FacesContext.getCurrentInstance();
            Map<String, String> map = ct.getExternalContext().getRequestParameterMap();
            String id = map.get("idParam");
            answers = answersDao.getObject(conn, Integer.parseInt(id));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return "delAnswer";
    }
}
